import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'
import HomeView from '../views/HomeView.vue'
import AuthView from '../views/AuthView.vue'

const routes = [
  {
    path: '/',
    component: HomeView,
    children: [
      { path: '', redirect: '/dashboard/workbench' },
      { path: 'dashboard/workbench', name: 'Workbench', component: () => import('../components/WorkbenchDashboard.vue') },
      { path: 'dashboard/analysis', name: 'Analysis', component: () => import('../views/PlaceholderView.vue') },
      { path: 'dashboard/ecommerce', name: 'Ecommerce', component: () => import('../views/PlaceholderView.vue') },
      { path: 'template/1', name: 'Template1', component: () => import('../views/PlaceholderView.vue') },
      { path: 'template/2', name: 'Template2', component: () => import('../views/PlaceholderView.vue') },
      { path: 'component/1', name: 'Component1', component: () => import('../views/PlaceholderView.vue') },
      { path: 'component/2', name: 'Component2', component: () => import('../views/PlaceholderView.vue') },
      { path: 'example/1', name: 'Example1', component: () => import('../views/PlaceholderView.vue') },
      { path: 'example/2', name: 'Example2', component: () => import('../views/PlaceholderView.vue') },
      { path: 'system/user', name: 'SystemUser', component: () => import('../views/UserManageView.vue') },
      { path: 'system/role', name: 'SystemRole', component: () => import('../views/RoleManageView.vue'), meta: { title: '角色管理' } },
      { path: 'system/permission', name: 'PermissionManage', component: () => import('../views/PermissionManageView.vue'), meta: { title: '权限管理' } },
      // 新增个人中心菜单
      { path: 'user/profile', name: 'UserProfile', component: () => import('../components/UserProfile.vue'), meta: { title: '个人中心' } },
      // 添加重定向，兼容/profile路径
      { path: 'profile', redirect: '/user/profile' },
      { path: 'article/list', name: 'ArticleList', component: () => import('../views/PlaceholderView.vue') },
      { path: 'article/edit', name: 'ArticleEdit', component: () => import('../views/PlaceholderView.vue') },
      { path: 'ops/monitor', name: 'OpsMonitor', component: () => import('../views/PlaceholderView.vue') },
      { path: 'ops/log', name: 'OpsLog', component: () => import('../views/PlaceholderView.vue') },
      { path: 'help', name: 'Help', component: () => import('../views/PlaceholderView.vue') },
      { path: 'changelog', name: 'Changelog', component: () => import('../views/PlaceholderView.vue') }
    ]
  },
  { path: '/auth', name: 'Auth', component: AuthView }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()

  // 检查是否有token
  if (!userStore.token) {
    if (to.path !== '/auth') {
      next({ path: '/auth' })
    } else {
      next()
    }
    return
  }

  // 检查token是否过期
  if (userStore.isTokenExpired) {
    userStore.logout()
    if (to.path !== '/auth') {
      next({ path: '/auth' })
    } else {
      next()
    }
    return
  }

  // 如果已登录且访问登录页，重定向到首页
  if (to.path === '/auth') {
    next({ path: '/' })
    return
  }

  // 其他情况正常通过
  next()
})

export default router
